package misc;
import ij.ImagePlus;
import ij.process.ImageProcessor;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class VideoCreator extends JPanel{

	/**
	 * This class was created to perform some processing on frames as removing isovist and some other manipulations
	 * so that the output frames are used to create a video.	 * 
	 * 
	 * */
	
	  private void removeNoise(ImageProcessor javaImage) {
			// TODO Auto-generated method stub
			for (int i = 0; i < javaImage.getWidth(); i++) {
				for (int j = 0; j < javaImage.getHeight(); j++) {
					int c = javaImage.get(i, j);
					int bb =  (c & 0xff);
					int gg =  ((c >> 8) & 0xff);
					int rr =  ((c >> 16) & 0xff);
					if(bb<250||gg<250||rr<250)
						javaImage.set(i, j, 0);
				}
				
			}
		  
		}
	
	public static void create(){
		
		final int length = 440; // 500 frames
		boolean skip = false;
		int cam1counter = 480;//93;
		int cam2counter = 411;//87;
		int cam3counter = 414;//75;
		int cam4counter = 400;//72;
		int mastercounter = 661;
		for (int i = 0; i < length; i++) {
			BufferedImage cam1Img = resizeImage(new ImagePlus("cam1"+cam1counter+".png").getBufferedImage(),320,240);
			BufferedImage cam2Img = resizeImage(new ImagePlus("cam_2_"+cam2counter+".png").getBufferedImage(),320,240);
			BufferedImage cam3Img = resizeImage(new ImagePlus("cam_3_1_"+cam3counter+".png").getBufferedImage(),320,240);
			BufferedImage cam4Img = resizeImage(new ImagePlus("cam_4_1_"+cam4counter+".png").getBufferedImage(),320,240);
			BufferedImage masterImg = new ImagePlus("test_"+mastercounter+".png").getBufferedImage();
			
			createImage(cam1Img,cam2Img,cam3Img,cam4Img,masterImg,i);
			if(skip){
				cam1counter++;
				cam2counter++;
				cam3counter++;
				cam4counter++;
				skip = false;
			}else
				skip = true;
			
			mastercounter++;
			System.out.println(i);
		}
	}
	
	private static void createImage(BufferedImage cam1Img,
			BufferedImage cam2Img, BufferedImage cam3Img,
			BufferedImage cam4Img, BufferedImage masterImg,int num) {
		// TODO Auto-generated method stub
		int width = 1280;
		int height = 960;
		BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
		Graphics g = img.createGraphics();
		
		g.drawImage(masterImg, width/4, height/4, width/2, height/2, null);
		g.drawImage(cam1Img, 0, 0, width/4, height/3, null);
		g.drawImage(cam2Img, (3*width/4), 0, width/4, height/3, null);
		g.drawImage(cam4Img, 0, 2*height/3, width/4, height/3, null);
		g.drawImage(cam3Img, (3*width/4), 2*height/3, width/4, height/3, null);
		try {
			ImageIO.write(img, "png", new File("output"+num+".png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void removeIsoVist(){
		int num = 255;
		LinkedList<Point>reds = new LinkedList<Point>();
		ImageProcessor masterImg = new ImagePlus("pic256.png").getProcessor();
		for (int i = 76; i < 320; i++) {
			for (int j = 60; j < 200; j++) {
				int c = masterImg.getPixel(i, j);
				int  r = (c&0xff0000)>>16;
				int  g = (c&0xff00)>>8;
				int   b = c&0xff;
				if(r==255 && g == 0 && b == 0){
					reds.add(new Point(i, j));
				}
			}
		}
		
		for (int i = 0; i < 1000; i++) {
			masterImg = new ImagePlus("pic"+num+".png").getProcessor();	
			for (int j = 0; j < 5; j++) {
				for (int j2 = 0; j2 < masterImg.getWidth(); j2++) {
					int c = masterImg.getPixel(j2, j);
					int  r = (c&0xff0000)>>16;
					int  g = (c&0xff00)>>8;
					int   b = c&0xff;
					if(r==255 && g == 0 && b == 0){
						int cc = masterImg.getPixel(j2, j+5);
						masterImg.putPixel(j2, j, cc);
					}
				}
			}
			
			for (int j = 0; j < masterImg.getHeight(); j++) {
				for (int j2 = 0; j2 <80 ; j2++) {
					int c = masterImg.getPixel(j2, j);
					int  r = (c&0xff0000)>>16;
					int  g = (c&0xff00)>>8;
					int   b = c&0xff;
					if(r==255 && g == 0 && b == 0){
						int cc = masterImg.getPixel(j2+5, j-5);
						masterImg.putPixel(j2, j, cc);
					}
				}
			}
			for (int j = 0; j < masterImg.getHeight(); j++) {
				for (int j2 = 290; j2 <masterImg.getWidth(); j2++) {
					int c = masterImg.getPixel(j2, j);
					int  r = (c&0xff0000)>>16;
					int  g = (c&0xff00)>>8;
					int   b = c&0xff;
					if(r==255 && g == 0 && b == 0){
						int cc = masterImg.getPixel(j2-1, j);
						masterImg.putPixel(j2, j, cc);
					}
				}
			}
			for (int j = 220; j < masterImg.getHeight(); j++) {
				for (int j2 = 0; j2 <masterImg.getWidth(); j2++) {
					int c = masterImg.getPixel(j2, j);
					int  r = (c&0xff0000)>>16;
					int  g = (c&0xff00)>>8;
					int   b = c&0xff;
					if(r==255 && g == 0 && b == 0){
						int cc = masterImg.getPixel(j2, j-1);
						masterImg.putPixel(j2, j, cc);
					}
				}
			}
			for (int j = 76; j < 300; j++) {
				for (int k = 60; k < 230; k++) {
					int c = masterImg.getPixel(j, k);
					int  r = (c&0xff0000)>>16;
					int  g = (c&0xff00)>>8;
					int   b = c&0xff;
					if(r==255 && g == 0 && b == 0){
						int cc = masterImg.getPixel(j, k-1);
						masterImg.putPixel(j, k,cc);
					}
				}
			}
			try {
				ImageIO.write(masterImg.getBufferedImage(), "png", new File("test_"+num+".png"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			num++;
		}
	}
		
	public static void putiso(){
		
		Scanner scan;
		
		
		int xpoints[] = new int[360];
		int ypoints[] = new int[360];
		for (int i = 256; i < 275; i++) {
			ImageProcessor masterImg = new ImagePlus("test_"+i+".png").getProcessor();
			try {
				scan = new Scanner(new File("ISo3.txt"));
				for (int j = 0; j < ypoints.length; j++) {
					String ll = scan.nextLine();
					String[] ls = ll.split(" ");
					xpoints[j] = Integer.parseInt(ls[0]);
					ypoints[j] = Integer.parseInt(ls[1]);
					
				}
				Polygon poly = new Polygon(xpoints, ypoints, ypoints.length);
				BufferedImage bb = masterImg.getBufferedImage();
				Graphics g = bb.getGraphics();
				g.setColor(Color.red);
				g.drawPolygon(poly);
				ImageIO.write(bb, "png", new File("test_"+i+".png"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for (int i = 275; i < 300; i++) {
			ImageProcessor masterImg = new ImagePlus("test_"+i+".png").getProcessor();
			try {
				scan = new Scanner(new File("ISo2.txt"));
				for (int j = 0; j < ypoints.length; j++) {
					String ll = scan.nextLine();
					String[] ls = ll.split(" ");
					xpoints[j] = Integer.parseInt(ls[0]);
					ypoints[j] = Integer.parseInt(ls[1]);
					
				}
				Polygon poly = new Polygon(xpoints, ypoints, ypoints.length);
				BufferedImage bb = masterImg.getBufferedImage();
				Graphics g = bb.getGraphics();
				g.setColor(Color.red);
				g.drawPolygon(poly);
				ImageIO.write(bb, "png", new File("test_"+i+".png"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for (int i = 300; i < 325; i++) {
			ImageProcessor masterImg = new ImagePlus("test_"+i+".png").getProcessor();
			try {
				scan = new Scanner(new File("ISo1.txt"));
				for (int j = 0; j < ypoints.length; j++) {
					String ll = scan.nextLine();
					String[] ls = ll.split(" ");
					xpoints[j] = Integer.parseInt(ls[0]);
					ypoints[j] = Integer.parseInt(ls[1]);
					
				}
				Polygon poly = new Polygon(xpoints, ypoints, ypoints.length);
				BufferedImage bb = masterImg.getBufferedImage();
				Graphics g = bb.getGraphics();
				g.setColor(Color.red);
				g.drawPolygon(poly);
				ImageIO.write(bb, "png", new File("test_"+i+".png"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for (int i = 325; i < 350; i++) {
			ImageProcessor masterImg = new ImagePlus("test_"+i+".png").getProcessor();
			try {
				scan = new Scanner(new File("ISo0.txt"));
				for (int j = 0; j < ypoints.length; j++) {
					String ll = scan.nextLine();
					String[] ls = ll.split(" ");
					xpoints[j] = Integer.parseInt(ls[0]);
					ypoints[j] = Integer.parseInt(ls[1]);
					
				}
				Polygon poly = new Polygon(xpoints, ypoints, ypoints.length);
				BufferedImage bb = masterImg.getBufferedImage();
				Graphics g = bb.getGraphics();
				g.setColor(Color.red);
				g.drawPolygon(poly);
				ImageIO.write(bb, "png", new File("test_"+i+".png"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	public static void addCameras(){
		for (int i = 257; i < 343; i++) {
			ImageProcessor masterImg = new ImagePlus("test_"+i+".png").getProcessor();
				BufferedImage bb = masterImg.getBufferedImage();
				Graphics g = bb.getGraphics();
				g.setColor(Color.green);
				g.drawRect(0, 19, 20, 20);
				g.drawRect(288, 15, 20, 20);
				try {
				ImageIO.write(bb, "png", new File("test_"+i+".png"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for (int i = 343; i < 382; i++) {
			ImageProcessor masterImg = new ImagePlus("test_"+i+".png").getProcessor();
				BufferedImage bb = masterImg.getBufferedImage();
				Graphics g = bb.getGraphics();
				g.setColor(Color.green);
				g.drawRect(287, 180, 20, 20);
				g.drawRect(288, 15, 20, 20);
				try {
				ImageIO.write(bb, "png", new File("test_"+i+".png"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for (int i = 383; i < 422; i++) {
			ImageProcessor masterImg = new ImagePlus("test_"+i+".png").getProcessor();
				BufferedImage bb = masterImg.getBufferedImage();
				Graphics g = bb.getGraphics();
				g.setColor(Color.green);
				g.drawRect(0, 186, 20, 20);
				g.drawRect(288, 15, 20, 20);
				try {
				ImageIO.write(bb, "png", new File("test_"+i+".png"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for (int i = 423; i < 656; i++) {
			ImageProcessor masterImg = new ImagePlus("test_"+i+".png").getProcessor();
				BufferedImage bb = masterImg.getBufferedImage();
				Graphics g = bb.getGraphics();
				g.setColor(Color.green);
				g.drawRect(0, 186, 20, 20);
				g.drawRect(287, 180, 20, 20);
				try {
				ImageIO.write(bb, "png", new File("test_"+i+".png"));
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
		  public static BufferedImage resizeImage(BufferedImage b,int w, int h) {
				// TODO Auto-generated method stub
				BufferedImage off_Image = 
						new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
				off_Image.createGraphics().drawImage(b, 0, 0,w,h,0,20,320, 260, null);
				return off_Image;
			}
	public static void removeArrow(){
		int num = 103;
		while(num<900){
		ImageProcessor img = new ImagePlus("vid"+num+".png").getProcessor();
		
		for (int i = 317; i < 340; i++) {
			for (int j = 238; j < 260; j++) {
				img.set(i, j, 0);
			}
		}
		try {
			ImageIO.write(img.getBufferedImage(), "png", new File("vid_"+num+".png"));
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		num++;
		}
		
	}
	public static void removeYellowAndBlue(){
		ImageProcessor img = new ImagePlus("D:\\Project\\project data\\out noiso\\test_440.png").getProcessor();
//		ImageProcessor img = new ImagePlus("D:\\2nd year\\Second Term\\workspace\\VideoTracking-1\\test_0.png").getProcessor();
		for (int i = 0; i < img.getWidth(); i++) {
			for (int j = 0; j < img.getHeight(); j++) {
				int val = img.get(i, j);
				int  r = (val&0xff0000)>>16;
				int  g = (val&0xff00)>>8;
				int   b = val&0xff;
				
				if((r==0 && g ==0 && b==255)||(r==255 && g ==255 && b==0)){
					int c = img.get(i, j-2);
					img.set(i, j,c);
				}
				
			}
			
		}
		BufferedImage bui = img.getBufferedImage();
		bui = resizeImage(bui,320,240);
		try {
			ImageIO.write(img.getBufferedImage(), "png", new File("vid_2.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void resize(){
		ImageProcessor img = new ImagePlus("D:\\2nd year\\Second Term\\workspace\\VideoTracking-1\\test_0.png").getProcessor();
				
		int k = 0;
		for (int i = 0; i < img.getWidth(); i+=3) {
			for (int j = 0; j < img.getHeight(); j++) {
				int r = img.get(i, j);
				int b = img.get(i+1, j);
				int g = img.get(i+2, j);
				
				int c = (r<<16 | b<<8 | g);
				img.set(k, j, c);
			}
			k++;
		}
		BufferedImage bui = img.getBufferedImage();
		bui = resizeImage(bui,320,280);
		try {
			ImageIO.write(bui, "png", new File("vid_2.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		create();
//		removeIsoVist();
//		putiso();
//		addCameras();
//		removeArrow();
//		removeYellowAndBlue();
		resize();
	}

}

